//
//  ActivityCell.m
//  iOS实战
//
//  Created by clz on 2019/10/26.
//  Copyright © 2019 clz. All rights reserved.
//

#import "ActivityCell.h"
#import <Masonry.h>
#import "Commont.h"
#import <UIImageView+WebCache.h>


@interface ActivityCell()

@property(nonatomic,strong)UIImageView *itemImageView;
@property(nonatomic,strong)UILabel *itemTitleLable;
@property(nonatomic,strong)UILabel *itemPriceLabel;
@property(nonatomic,strong)UIButton *itemJionButton;



@end

@implementation ActivityCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.activityCellVM = [[ActivityCellVM alloc]init];
        [self.activityCellVM addObserver:self forKeyPath:@"activityImg" options:NSKeyValueObservingOptionNew context:nil];
        [self.activityCellVM addObserver:self forKeyPath:@"activityName" options:NSKeyValueObservingOptionNew context:nil];
        [self.activityCellVM addObserver:self forKeyPath:@"fee" options:NSKeyValueObservingOptionNew context:nil];

        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.itemImageView = [[UIImageView alloc]init];
        self.itemImageView.image = [UIImage imageNamed:@"default_banner"];
        self.itemImageView.clipsToBounds = YES;
        self.itemImageView.layer.cornerRadius = 4.0f;
        [self.contentView addSubview:self.itemImageView];
        [self.itemImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(148, 84));
            make.left.equalTo(self.contentView).offset(20);
            make.top.equalTo(self.contentView).offset(20);
            make.bottom.equalTo(self.contentView).offset(-20);
        }];
        
        self.itemTitleLable = [[UILabel alloc]init];
        self.itemTitleLable.text = @"测试itemPriceLabelitemPriceLabel测试测试测试测试测试测试测试测试测试";
        self.itemTitleLable.font = [UIFont boldSystemFontOfSize:15];
        self.itemTitleLable.textColor = RGBHex(0x263238);
        self.itemTitleLable.numberOfLines = 2;
        [self.contentView addSubview:self.itemTitleLable];
        [self.itemTitleLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.itemImageView.mas_right).offset(11.5 - 3);
            make.top.equalTo(self.itemImageView);
            make.right.equalTo(self.contentView).offset(-20 + 3);
        }];
        
        self.itemPriceLabel = [[UILabel alloc]init];
        self.itemPriceLabel.text = @"测试价格";
        self.itemPriceLabel.font = [UIFont boldSystemFontOfSize:14];
        self.itemPriceLabel.textColor = RGBHex(0x21B6B4);
        [self.contentView addSubview:self.itemPriceLabel];
        [self.itemPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.itemTitleLable);
            make.bottom.equalTo(self.itemImageView);
        }];
        
        self.itemJionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.itemJionButton setTitle:@"立即报名" forState:UIControlStateNormal];
        [self.itemJionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.itemJionButton.titleLabel.font = [UIFont boldSystemFontOfSize:13];
        [self.itemJionButton setBackgroundColor:RGBHex(0xFF5A5A)];
        self.itemJionButton.layer.cornerRadius = 2;
        [self.itemJionButton addTarget:self action:@selector(jionAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.itemJionButton];
        [self.itemJionButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(72, 28));
            make.right.equalTo(self.itemTitleLable);
            make.bottom.equalTo(self.itemPriceLabel);
        }];
        
        
        
        
    }
    return self;
}

- (void)jionAction:(UIButton *)sender{
    if (self.joinBlock) {
        self.joinBlock();
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    id value = change[NSKeyValueChangeNewKey];
    
    if ([keyPath isEqualToString:@"activityImg"]) {
        NSString *activityImg = value;
        [self.itemImageView sd_setImageWithURL:[NSURL URLWithString:activityImg] placeholderImage:[UIImage imageNamed:@"default_banner"]];
        
    }else if ([keyPath isEqualToString:@"activityName"]){
        NSString *activityName = value;
        self.itemTitleLable.text = activityName;
        
    }else if ([keyPath isEqualToString:@"fee"]){
        NSString *fee = value;
        self.itemPriceLabel.text = [NSString stringWithFormat:@"HK$:%@",fee];
    }
}

- (void)dealloc{
    [self.activityCellVM removeObserver:self forKeyPath:@"activityImg"];
    [self.activityCellVM removeObserver:self forKeyPath:@"activityName"];
    [self.activityCellVM removeObserver:self forKeyPath:@"fee"];

}


@end
